package com.qd.panda.applyfor;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.exception.ApiException;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.panda.domain.entity.applayfor.ApplyForParkDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.applyfor.ApplyForParkService;
import com.qd.panda.service.applyfor.StateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 短期租赁/WEB_点亮小区
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/applyForPark/")
@Slf4j
public class ApplyForParkController {
    private final ApplyForParkService applyForParkService;


    /**
     * 处理点亮
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:applyForPark:edit')")
    @OperatorInfo(title = "处理点亮", operator = OperatorType.UPDATE)
    @PutMapping(value = "/handle")
    public ApplyForParkDTO handle(@Validated @RequestBody ApplyForParkDTO dto) {
        if (!StringUtils.hasLength(dto.getId())) {
            throw new ApiException("ID不能为空");
        }
        if (null == dto.getState()) {
            throw new ApiException("状态不能为空");
        }
        if (!dto.getState().equals(StateUtil.STATE_1) && !dto.getState().equals(StateUtil.STATE_2)) {
            throw new ApiException("状态值错误");
        }
        dto.setHandlePersonId(SecurityUtils.getLoginUser().getUserId());
        dto.setHandleOrgId(SecurityUtils.getLoginUser().getCurrentOrgId());
        dto.setHandleTime(LocalDateTime.now());
        return applyForParkService.handle(dto);
    }

    /**
     * 获取点亮
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public ApplyForParkDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return applyForParkService.getByIdDto(id);
    }

    /**
     * 点亮列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<ApplyForParkDTO> page(ApplyForParkDTO dto) {
        return applyForParkService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 删除点亮小区
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:applyForPark:delete')")
    @OperatorInfo(title = "删除", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            applyForParkService.removeById(ids.get(0));
        } else {
            applyForParkService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }
}